package com.jm.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.jm.dao.LoginDao;
import com.jm.dao.Utils;
import com.jm.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
//	String username;
//	String password;
	String mKey;
	public Map<String, Object> responseJson;
	public Map<String, String> loginOutStr;
	public Map<String, Object> changeWordsMap;

	Map<String, Object> sessions = ActionContext.getContext().getSession();
	
	
	public Map<String, String> getLoginOutStr() {
		return loginOutStr;
	}
	public void setLoginOutStr(Map<String, String> loginOutStr) {
		this.loginOutStr = loginOutStr;
	}
	public Map<String, Object> getResponseJson() {  
        return responseJson;  
    } 
	public void setResponseJson(Map<String, Object> res)
	{
		this.responseJson = res;
	}
    //执行登录的方法
    public String execute() throws IOException, NoSuchAlgorithmException{
    	HttpServletRequest request = ServletActionContext.getRequest(); 
    	
    	String username = request.getParameter("username");
    	String password = request.getParameter("password");
    	
    	return valideLoginInfo(username, password);
    }
    
    //change password
    public String chagePassWord(){
    	try {
			changeWordsMap = new HashMap<>();
			HttpServletRequest request = ServletActionContext.getRequest();
			String uid = String.valueOf(request.getParameter("uid"));
			String password = String.valueOf(request.getParameter("password"));
			if (Utils.isNumeric(uid)) {
				boolean result = new LoginDao().modifyPassWord(Utils.EncoderByMd5(String.valueOf(password)), Long.valueOf(uid));
				if (result) {
					changeWordsMap.put("result", true);
				}else{
					changeWordsMap.put("result", false);
				}
			}else {
				changeWordsMap.put("result", false);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			changeWordsMap.put("result", false);
		}
    	return SUCCESS;
    }
    
    public String valideLoginInfo(String username,String pwd) throws NoSuchAlgorithmException, UnsupportedEncodingException{
    	Map<String,Object> res = new HashMap<String,Object>();
    	LoginDao mLoginDao = new LoginDao();
    	if(username == null || pwd ==null){
    		this.setResponseJson(res);
    		res.put("msg", "用户名密码不能为空");
    		return Action.SUCCESS;
    	}
    	com.jm.viewmodel.User user = mLoginDao.checkUser(username, Utils.EncoderByMd5(pwd)); 
    	if(user!=null && user.getRet()=="1"){
    		res.put("msg", "用户名密码错误");
    		return Action.SUCCESS;
    	}
    	if (user!=null &&user.getIsAuthorit()!=null){
    		System.out.println(user.getIsAuthorit());
	    	if (user.getIsAuthorit()== "0"){
	    		user = null;
	    		res.put("msg", "用户未授权");
	    	}else{
	    		res.put("result", user);
	    	}
    	}else
    	{
    		res.put("msg", "用户不存在");
    	}
    
    	this.setResponseJson(res);
    	return Action.SUCCESS;
    }
    
    public String loginOut(){
    	if(sessions.get("user").equals(mKey)){
    		sessions.put("user", "");
    		loginOutStr.put("result", "0");
    		this.setLoginOutStr(loginOutStr);
    	}
    	return SUCCESS;
    }
}
